#include <stdio.h>
#include <stdlib.h>
#define maxsize 50

//定义顺序表结构体
typedef struct SqList
{
    int data[maxsize];
    int length;
}SqList;

SqList function(SqList L){
    SqList s;
    s.length = 0;
    int t = 0;//用于赋值
    int i = 0;
    int j = L.length - 1;//用于定位
    for(;i<=j;)
    {
        if(s.length+1 == L.length) {
            s.data[t] = L.data[i];
            s.length++;
            break;
        }
            
        s.data[t] = L.data[i];
        s.data[t+1] = L.data[j];
        s.length += 2;
        t += 2;
        i++;
        j--;        
    }
    return s;
}

SqList function2(SqList l)
{
    // 新建链表
    SqList s;
    s.length = 0;
    // 访问 i j 
    int i = 0; 
    int j = l.length-1;

    for(; i <= j; i--, j++) {
        if(i == j) {
            s.data[s.length++] = l.data[i];
        } else {
            s.data[s.length++] = l.data[i];
            s.data[s.length++] = l.data[j];
        }
    }
    return s;
}

// 双指针：多个指针多个数组，多个指针一个数组（同向，相向）

//目标值为两数之和，返回下标
void function3(SqList L,int t,int &p,int &q){
    //双指针，相向
    int i = 0;
    int j = L.length - 1;
    int sum;
    for (; i < j; )
    {
        sum = L.data[i] + L.data[j];
        if (sum == t)
        {
            p = i;
            q = j;
            break;
        }
        if (sum < t)
        {
            i++;
        }
        else j--;    
    }
}


//合并两个递增数组，长为m,n
void merge(int num1[],int num2[], int m, int n, int *num){
    int i = 0;
    int j = 0;
    int t = 0;
    for (; i<m && j<n;)
    {
        if (num1[i] < num2[j])
            num[t++] = num1[i++];
        else
            num[t++] = num2[j++];
    }
    if (j<n)
        for(;j<n;num[t++] = num2[j++])
    if (i<m)
        for(;i<m;num[t++] = num1[i++])
    return;
}
int main(){
    SqList l;
    l.length = 0;
    for(int  i = 0; i < 7; i++) {
        l.data[i] = i+1;
        l.length++;
    }
    for(int  i = 0; i <l.length; i++) {
        printf("%d ", l.data[i]);
    }
    printf("\n");
    SqList s = function(l);
    for(int  i = 0; i <s.length; i++) {
        printf("%d ", s.data[i]);
    }

    // merge
    printf("\n");
    int num1[] = {1,3,5,7,9};
    int num2[] = {2,4,6,8,10};
    int res[10];
    merge(num1, num2, 5, 5, res);
    for(int i = 0; i < 10; i++) {
        printf("%d ", res[i]);
    }
    return 0;
}

